﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectTemplate.Common.Response;
using ProjectTemplate.IServices;
using ProjectTemplate.Model.DTO.Category;
using ProjectTemplate.Model.Entities;

namespace ProjectTemplate.Controllers
{
    /// <summary>
    /// 分类控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class CategoryController : ControllerBase
    {
        private readonly ICategoryService _categoryService;

        public CategoryController(ICategoryService categoryService)
        {
            _categoryService = categoryService;
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<long>> AddCategory(CategoryAddDTO dto)
        {
            var id = await _categoryService.AddCategory(dto);

            return ResultBuilder.SuccessResult(id);

        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<Response<bool>> DeleteCategory(long id)
        {
            bool result = await _categoryService.DeleteCategory(id);

            return ResultBuilder.SuccessResult(result);

        }

        /// <summary>
        /// 修改分类
        /// </summary>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<Response<bool>> UpdateCategory(long id, CategoryUpdateDTO dto)
        {
            bool result = await _categoryService.UpdateCategory(id, dto);

            return ResultBuilder.SuccessResult(result);

        }

        /// <summary>
        /// 分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<Category>>> GetCategories()
        {
            var categories = await _categoryService.GetCategories();

            return ResultBuilder.SuccessResult(categories);

        }
    }
}
